<!-- 飞机 -->
<template>
  <el-dialog :modelValue="show" :title="GameName(indexStore.gameArray, 100001) + $t('381')" class="hund-dialog"
    top="10vh" :close-on-click-modal="false" :close-on-press-escape="false" @close="onHideDetails">
    <el-form :model="data" ref="ruleFormRef" :validate-on-rule-change="false"
      :disabled="isDisableUser">
      <form-item :title="t('41')" value="game_name">
        <div class="gameIcon">
          <div class="flex marginR5">
            <el-tag size="small">{{ GamePlatform(indexStore.gameArray, 100001) }}</el-tag>
            <img class="game-icon" :src="selectToLabel(GAMEICON, 100001)" alt="">
          </div>
          {{ GameName(indexStore.gameArray, 100001) }}
        </div>
      </form-item>
      <form-item :title="$t('249')" :pass="true" value="a_hundred_profitmargin">
        <el-select v-model="data.a_hundred_profitmargin" filterable>
          <el-option v-show="item" v-for="item in 5" :label="item + '%'" :value="item" />
        </el-select>
        <div class="input-text">{{ $t('250') }}</div>
      </form-item>
      <form-item :title="$t('956')" :pass="true" value="a_hundred_crash_rate">
        <el-input-number v-model="data.a_hundred_crash_rate" :min="0" :max="100" :precision="2"
          :placeholder="$t('956')" clearable controls-position="right" />%
        <div class="input-text">{{ $t('957') }}</div>
      </form-item>
      <form-item :title="$t('260')" class="yxxz-item" value="a_hundred_stake">
        <div class="flexWrap margin5" v-for="(item, index) in data.gameStakeList" :key="index">
          <div class="xz-label">{{ $t('251') }} </div>
          <el-input-number size="large" v-model="item.scale" :min="0.01" :max="99999999999999999999" :precision="2" 
          class="marginT10" clearable controls-position="right" />
          <div class="input-text">{{ $t('252') }}</div>

          <div class="xz-label">{{ $t('253') }} </div>
          <el-input-number size="large" v-model="item.min" :min="0" :max="99999999999999999999" :precision="0"
            class="marginT10" clearable controls-position="right" />
          <div class="input-text">{{ $t('254') }}</div>

          <div class="xz-label">{{ $t('255') }} </div>
          <el-input-number size="large" v-model="item.max" :min="0" :max="99999999999999999999" :precision="0"
            class="marginT10" clearable controls-position="right" />
          <div class="input-text">{{ $t('256') }}</div>

          <div class="xz-label">{{ $t('257') }}</div>
          <el-input-number size="large" v-model="item.default" :min="0" :max="99999999999999999999" 
            :precision="0" class="marginT10" clearable controls-position="right" />
          <div class="input-text">{{ $t('258') }}</div>

          <div class="xz-label">{{ $t('259') }}</div>
          <el-input-number size="large" v-model="item.winMax" :min="0" :max="99999999999999999999"
            :precision="0" class="marginT10" clearable controls-position="right" />
          <div class="input-text">{{ $t('261') }}</div>

          <div class="xz-label">{{ $t('262') }}</div>
          <el-input-number size="large" v-model="item.fast1" :min="0" :max="99999999999999999999"
            :precision="0" class="marginT10" clearable controls-position="right" />
          <div class="input-text">{{ $t('263') }}</div>

          <div class="xz-label"> {{ $t('264') }}</div>
          <el-input-number size="large" v-model="item.fast2" :min="0" :max="99999999999999999999"
            :precision="0" class="marginT10" clearable controls-position="right" />
          <div class="input-text">{{ $t('263') }}</div>

          <div class="xz-label">{{ $t('265') }}</div>
          <el-input-number size="large" v-model="item.fast3" :min="0" :max="99999999999999999999"
            :precision="0" class="marginT10" clearable controls-position="right" />
          <div class="input-text">{{ $t('263') }}</div>

          <div class="xz-label">{{ $t('266') }}</div>
          <el-input-number size="large" v-model="item.fast4" :min="0" :max="99999999999999999999"
            :precision="0" class="marginT10" clearable controls-position="right" />
          <div class="input-text">{{ $t('263') }}</div>
        </div>
      </form-item>
    </el-form>

    <template #footer>
      <div class="dialog-footer" v-show="!props.isDisableUser">
        <el-button @click="onHideDetails">{{ $t('240') }}</el-button>
        <el-button type="primary" @click="gameSet(ruleFormRef)">{{ $t('241') }}</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script lang="ts" setup>
import { ref, reactive, watch } from 'vue'
import { ElMessage } from 'element-plus'
import type { FormInstance, FormRules } from 'element-plus'
import FormItem from '@/components/FormItem/FormItem.vue'
import { GAMEICON } from '@/config/eunm'
import { BigDiv, BigTimes } from '@/utils/math'
import { throttle, selectToLabel, GamePlatform, GameName } from '@/utils/index'
import { getHundredConfig, updateHundredConfig } from '@/api/settion'

import { useI18n } from 'vue-i18n'
const { t } = useI18n()
import { useStore } from '@/stores/index'
const indexStore = useStore()

const props = defineProps({
  show: Boolean, //  弹框显示
  isDisableUser: {
    type: Boolean,
    default: () => {
      return false
    }
  },
  checkedItem: {
    type: Object,
    default: () => {
      return {}
    }
  },
})

const data = reactive({
  id: 0,
  a_hundred_crash_rate: 0, // 飞机坠毁控制配置
  a_hundred_profitmargin: 3, // 利润率
  a_hundred_stake: '', // 游戏下注
  gameStakeList: [] as any[], // 游戏下注
  defaultBetMultiple: 1, //飞机下注额倍数 配置的额度上限值是默认飞机配置的多少倍【游戏下注上限】
  defaultBetStake: [] as any[], //默认-下注额度 配置的额度上限值是默认飞机配置的多少倍【游戏下注上限】
  defaultBetPool: [] as any[], //默认-水位配置 配置的额度上限值是默认飞机配置的多少倍【游戏下注上限】
})

watch(
  () => [props.checkedItem, props.show],
  async (value) => {
    if (value[0]) {
      Object.assign(data, props.checkedItem)
    }
    if (value[1]) {
      chooseOperator(props.checkedItem.agent_id, props.checkedItem.game_id) // 获取运营商配置
    }
  }
)

// 选择运营商
const chooseOperator = async (id: number, game_id: number) => {
  let res = await getHundredConfig({ agent_id: id, game_id: game_id });
  Object.assign(data, res.data)
  // 计算飞机下注额倍数
  let oldData = await getHundredConfig({ agent_id: 0, game_id: props.checkedItem.game_id }) as any;
  data.defaultBetMultiple = oldData.data.gameStakeList[0].max
  // 下注额度
  data.defaultBetStake =  oldData.data.robotAmountList
  // 水位配置
  data.defaultBetPool =  oldData.data.poolList
}

// 保存
const ruleFormRef = ref<FormInstance>()
const rules = reactive<FormRules<typeof data>>({
  a_hundred_crash_rate: [{ required: true, message: t('19')+t('956'), trigger: 'blur' }],
  a_hundred_profitmargin: [{ required: true, message: t('19')+t('249'), trigger: 'blur' }]
})
const gameSet = throttle(async (formEl: FormInstance | undefined) => {
  if (!formEl) return
  await formEl.validate(async (valid) => {
    if (valid) {
      // 游戏下注---默认值不能小于最小值
      if(data.gameStakeList[0].min > data.gameStakeList[0].default){
        return ElMessage.error(t('1029'))
      }
      // 游戏下注---默认值不能大于最大值
      if(data.gameStakeList[0].default > data.gameStakeList[0].max){
        return ElMessage.error(t('1030'))
      }
      // 游戏下注---最小值不能大于最大值的10分之一
      if(data.gameStakeList[0].min > BigDiv(data.gameStakeList[0].max,10)){
        return ElMessage.error(t('1028'))
      }
      // 游戏下注---配置的额度上限值是默认飞机配置的多少倍【游戏下注上限】
      let multiple = BigDiv(data.gameStakeList[0].max,data.defaultBetMultiple) //倍数
      // 下注额度
      data.defaultBetStake.forEach((item:any)=>{
        item.betAmount.forEach((second:any)=>{
          second.min = BigTimes(second.min,multiple)
          second.max = BigTimes(second.max,multiple)
        })
      })
      data.robotAmountList = data.defaultBetStake
      // 水位配置
      data.defaultBetPool.forEach((item:any)=>{
        item.min = BigTimes(item.min,multiple)
        item.max = BigTimes(item.max,multiple)
      })
      data.poolList = data.defaultBetPool 
      console.log(data);
      await updateHundredConfig(data);
      ElMessage.success(t('342'))
      emit('onHideDetails', true)
    }
  })
}, 3000)

const changeNum = (val: number, name: any) => {
  if (!val) {
    data.gameStakeList[0][name] = 0
  }
}

// 取消
const emit = defineEmits(['onHideDetails'])
const onHideDetails = () => {
  emit('onHideDetails', false)
}
</script>
<style scoped lang="scss">
.input-text {
  display: block;
  flex-basis: 100%;
  font-size: 12px;
  border-bottom: 1px solid var(--el-border-color-lighter);
  padding-left: 10px;
}

.xz-label {
  width: 220px;
  font-size: 16px;
  font-weight: bold;
  padding: 15px 0 0 10px
}
</style>